/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/
#include <QIcon>
#include <QSettings>

#include "menumodel.h"

MenuModel::MenuModel(QObject *parent)
		: QAbstractItemModel(parent), m_menuRoot(0)
{
	QSettings s;
	m_menuFont=s.value("meniu/font",QFont()).value<QFont>();
	m_menuPluginFont=s.value("meniu/font_modul",QFont()).value<QFont>();
}

int MenuModel::columnCount(const QModelIndex & parent) const
{
	Q_UNUSED(parent);
	return 1;
}

int MenuModel::rowCount(const QModelIndex & parent) const
{

	if (parent.isValid())
	{
		if (parent.internalPointer())
			return reinterpret_cast<MenuStruct*>(parent.internalPointer())->children.size();
	}
	else
		if (m_menuRoot)
			return m_menuRoot->children.size();
	return 0;
}

QVariant MenuModel::data(const QModelIndex & index, int role) const
{
	QVariant var;
	if (!reinterpret_cast<MenuStruct *>(index.internalPointer()))
		return var;
	switch (role)
	{
		case Qt::DisplayRole:
			var = reinterpret_cast<MenuStruct *>(index.internalPointer())->text;
			break;
		case Qt::DecorationRole:
			if (reinterpret_cast<MenuStruct *>(index.internalPointer())->plugin)
				var = reinterpret_cast<MenuStruct *>(index.internalPointer())->plugin->icon(reinterpret_cast<MenuStruct *>(index.internalPointer())->fullMenu).isNull()?QIcon(":/images/tux.png"):reinterpret_cast<MenuStruct *>(index.internalPointer())->plugin->icon(reinterpret_cast<MenuStruct *>(index.internalPointer())->fullMenu);
			else
				var = QIcon(":/images/arrow-right-double.png");
			break;
		case Qt::ToolTipRole:
		case Qt::StatusTipRole:
		case Qt::WhatsThisRole:
			if (reinterpret_cast<MenuStruct *>(index.internalPointer())->plugin)
				var = reinterpret_cast<MenuStruct *>(index.internalPointer())->plugin->toolTip(reinterpret_cast<MenuStruct *>(index.internalPointer())->fullMenu);
			else
				var = tr("Click pentru mai mult");
			break;
		case Qt::FontRole:
			if (reinterpret_cast<MenuStruct *>(index.internalPointer())->plugin)
				var = m_menuPluginFont;
			else
				var = m_menuFont;
			break;
	}
	return var;
}

QModelIndex MenuModel::index(int row, int column, const QModelIndex & parent) const
{
	if (parent.isValid())
	{
		if (reinterpret_cast<MenuStruct *>(parent.internalPointer()) && row < reinterpret_cast<MenuStruct *>(parent.internalPointer())->children.size())
		{
			reinterpret_cast<MenuStruct *>(parent.internalPointer())->children[row]->parentIndex = parent;
			return createIndex(row, column, reinterpret_cast<MenuStruct *>(parent.internalPointer())->children[row]);
		}
		return QModelIndex();
	}
	else
		if (m_menuRoot && row < m_menuRoot->children.size())
		{
			m_menuRoot->children[row]->parentIndex = parent;
			return createIndex(row, column, m_menuRoot->children[row]);
		}
	return QModelIndex();
}

QModelIndex MenuModel::parent(const QModelIndex & index) const
{
	if (index.internalPointer())
		return reinterpret_cast<MenuStruct *>(index.internalPointer())->parentIndex;
	return QModelIndex();
}

MenuModel::MenuStruct * MenuModel::addToMenu(MenuStruct * root, const QString & submenu, MenuWidgetInterface * plugin)
{
	if (!m_menuRoot)
		m_menuRoot = new MenuStruct;

	if (!root)
		root = m_menuRoot;

	foreach(MenuStruct * ms, root->children)
		if (submenu == ms->text && ms->plugin == plugin)
			return ms;

	MenuStruct * submenus = new MenuStruct;
	submenus->text = submenu;
	submenus->plugin = plugin;
	root->children.push_back(submenus);
	return submenus;
}

void MenuModel::addMenu(MenuWidgetInterface * widget)
{
	foreach(QString menu, widget->menus())
	{
		QStringList menus;
		foreach(QString submenu, menu.split('/'))
			if (submenu.size())
				menus<<submenu;

		if (!menus.size())
			continue;

		if(!m_menus.contains(menus[0]))
			continue;

		MenuStruct * root = m_menuRoot;

		foreach(QString submenu, menus)
			if (submenu.size())
				root = addToMenu(root, submenu, 0);

		if (root)
		{
			root->plugin = widget;
			root->fullMenu=menu;
		}
	}
	reset();
}

void MenuModel::setMenus(const QStringList & menus)
{
	m_menus=menus;
}

void MenuModel::deleter(MenuStruct * ms)
{
	if (!ms)
		return;
	foreach(MenuStruct * dms, ms->children)
		deleter(dms);
	delete ms;
}

MenuModel::~MenuModel()
{
	deleter(m_menuRoot);
}
